import 'package:flutter/material.dart';
import 'package:hao_ke/pages/auth/auth.dart';
import 'package:hao_ke/pages/utils/scoped_model_helper.dart';
import 'package:hao_ke/utils/shared_preferences.dart';

import '../../consts/consts.dart';

class My extends StatelessWidget {
  const My({super.key});

  @override
  Widget build(BuildContext context) {
    return MediaQuery(
      data: MediaQueryData(),
      child:  Scaffold(
        primary: false,
        body: Container(
          decoration: BoxDecoration(
            color: Consts.content_color,
            gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  Color.fromRGBO(200, 100, 100, 0.5),
                  Color.fromRGBO(0, 0, 200, 0.5),
                ]),
          ),
          child: Column(
            children: [
              Header(),
              SizedBox(height: 10,),
              FunctionButton(),
            ],
          ),
        ),
      ),
    );
  }
}

class Header extends StatefulWidget {

  const Header({super.key});

  @override
  State createState() {
    // TODO: implement createState
    return _HeaderState();
  }
}

class _HeaderState extends State<Header> {

  @override
  Widget build(BuildContext context) {

    return Container(
      padding: const EdgeInsets.only(top: 110,bottom: 20,left: 10,right: 10),
      height: 200,
      decoration: BoxDecoration(
        color: Color.fromRGBO(200, 200, 200, 0.4),
        borderRadius: BorderRadius.only(bottomLeft: Radius.circular(10),bottomRight: Radius.circular(10)),
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Image.asset("images/a-Group1308.png", width: 70),
          const SizedBox(
            width: 10,
          ),
          GestureDetector(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  ScopedModelHelper.getModel<AuthModel>(context).isLogin ? ScopedModelHelper.getModel<AuthModel>(context).name : '登录/注册',
                  style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                ),
                Container(
                    width: 210,
                    child: Text(
                      ScopedModelHelper.getModel<AuthModel>(context).isLogin?'欢迎回来_亲爱的护理师大人!':'登录后可以体验更多功能',
                        overflow: TextOverflow.ellipsis,style: TextStyle(color: Colors.black54,fontSize: 16),))
                ,
              ],
            ),
            onTap: () {
              if (!ScopedModelHelper.getModel<AuthModel>(context).isLogin) {
                Navigator.of(context).pushReplacementNamed('login');
                return;
              }
              ScopedModelHelper.getModel<AuthModel>(context).logout();
            },
          ),
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _checkIsLogin();
    });
  }

  void _checkIsLogin() async {
    print(ScopedModelHelper.getModel<AuthModel>(context).token);
  }
}

class FunctionButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(10),
      child: ListBody(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              FunctionButtonItem("images/a-Group1287.png","看房记录"),
              FunctionButtonItem("images/a-Group1288.png","我的订单"),
              FunctionButtonItem("images/a-Group1289.png","我的收藏"),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              FunctionButtonItem("images/a-Group1291.png","身份认证"),
              FunctionButtonItem("images/a-Group1292.png","联系我们"),
              FunctionButtonItem("images/a-Group1293.png","电子合同"),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              FunctionButtonItem("images/a-Group1294.png","房屋管理"),
              FunctionButtonItem("images/a-Group1295.png","钱包"),
              FunctionButtonItem("images/a-Group1296.png","更多"),
            ],
          ),
        ],
      ),
    );
  }
}


class FunctionButtonItem extends StatelessWidget{

  final String image;

  final String title;


  FunctionButtonItem(this.image, this.title);

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Container(
        decoration: BoxDecoration(
          color: Color.fromRGBO(200, 200, 200, 0.3),
          borderRadius: BorderRadius.circular(10),
        ),
        height: 80,
        margin: EdgeInsets.all(1),
        padding: EdgeInsets.all(10),
        child: Column(
          children: [
            Image.asset(image,width: 40,),
            Text(title),
          ],
        ),
      ),
    );
  }
  
}
